package com.block.datapush.service.apipush;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.BeiJingGJJUtil;
import com.block.datapush.service.apipush.utils.YouXinAesUtils;
import com.block.datapush.service.apipush.utils.YouXinUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 北京公积金
 */
@Service
public class BeiJingGJJApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;

    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);

        String queryUrl = pushApiKeyParam.getCheckUrl();
        String phone = memberSourceInfo.getMobileMd5();


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("enmobile", phone);
        paramMap.put("key", pushApiKeyParam.getApiKey());


        log.info("【北京公积金】撞库开始【queryUrl】{}【bizMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .form(paramMap)
                .execute().body();
        log.info("【北京公积金】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer errNum = resultObject.getInteger("errNum");
        String errMsg = resultObject.getString("errMsg");

        pushApiResult.setCode(errNum + "|" + errMsg);
        pushApiResult.setMessage(errMsg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(errNum), "0")) {
            pushApiResult.setPushSuccess(true);
        }
        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();

        return pushApiResult;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);

        String requestUrl = pushApiKeyParam.getRegisterUrl();

        String city = memberSourceInfo.getCity();

        if (StrUtil.endWith(city, "市")) {
            city = memberSourceInfo.getCity().substring(0, memberSourceInfo.getCity().length() - 1);
        }


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("mobile", memberSourceInfo.getMobile());
        paramMap.put("name", memberSourceInfo.getRealname());
        paramMap.put("age", memberSourceInfo.getAge());
        paramMap.put("city", city);
        paramMap.put("money", BeiJingGJJUtil.getloan(memberSourceInfo.getLoanAmount()));
        paramMap.put("key", pushApiKeyParam.getApiKey());
        paramMap.put("sex", memberSourceInfo.getSex() == 1 ? "男" : "女");
        paramMap.put("has_house", BeiJingGJJUtil.getHouse(memberSourceInfo.getHouse()));
        paramMap.put("has_car",BeiJingGJJUtil.getCar(memberSourceInfo.getCar()) );
        paramMap.put("has_baodan",BeiJingGJJUtil.getbaodan(memberSourceInfo.getBaoxian()));
        paramMap.put("has_gongjijin",BeiJingGJJUtil.getGjj(memberSourceInfo.getGjj()));
        paramMap.put("has_weilidai","无");
        paramMap.put("has_nasui","无");
        paramMap.put("remark","客户芝麻分："+BaseTypeInfoEnum.Zhima.getNameByValue(memberSourceInfo.getZhima())+"   |   "
                +"客户社保：" + BaseTypeInfoEnum.Shebao.getNameByValue(memberSourceInfo.getShebao())
                +"客户白条："+BaseTypeInfoEnum.Baitiao.getNameByValue(memberSourceInfo.getBaitiao())
                +"客户逾期情况"+BaseTypeInfoEnum.Yuqi.getNameByValue(memberSourceInfo.getYuqi()));


        log.info("【北京公积金】导单开始【requestUrl】{}【paramMap】{}", requestUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(requestUrl)
                .form(paramMap)
                .execute().body();
        log.info("【北京公积金】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer errNum = resultObject.getInteger("errNum");
        String errMsg = resultObject.getString("errMsg");


        pushApiResult.setCode(errNum + "|" + errMsg);
        pushApiResult.setMessage(errMsg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(errNum), "0")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        return pushApiResult;
    }

    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }


    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        return null;
    }
}
